﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for Email
/// </summary>
public class Email
{
    public Email()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public string SendingMail(string MailName, string MailTo, string MailForm, string MailCC, string MailBCC, string Subject, string MailBody)
    {
        #region Taking Values To Local String

        string EmailName = MailName;
        string EmailTo = MailTo;
        string EmailForm = MailForm;
        string EmailCC = MailCC;
        string EmailBCC = MailBCC;
        string EmailSubject = Subject;
        string EmailBody = MailBody;

        string Msg = string.Empty;

        #endregion

        //Email sending     
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress(EmailForm, EmailName);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = System.Configuration.ConfigurationManager.AppSettings["MailServer"];

            //Default port will be 25
            // smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            if (EmailTo != null && EmailTo != "")
            {
                message.To.Add(EmailTo);
            }
            message.Subject = EmailSubject;

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            if (EmailCC != null && EmailCC != "")
            {
                message.CC.Add(EmailCC);
            }
            //message.CC.Add("admin2@yoursite.com");

            //// You can specify Address directly as string
            if (EmailBCC != null && EmailBCC != "")
            {
                message.Bcc.Add(EmailBCC);
            }
            //message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
            //message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = true;

            // Message body content
            message.Body = EmailBody;

            // Send SMTP mail
            smtpClient.Send(message);

            Msg = "Thank you For Contact Us.";
        }
        catch (Exception ex)
        {
            Msg = "Send Email Failed." + ex.Message;
        }
        return Msg;
    }
}
